package com.gs.view.riepilogo;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.gs.R;
import com.gs.business.cache.CacheUscite;
import com.gs.domain.SingleSpesa;
import com.gs.view.uscite.UsciteView;
import comi.gsi.view.MyBaseActivity;

public class ListaUscite extends MyBaseActivity {

	@Override
	protected int getIdLayout() {
		return R.layout.listattivita;
	}

	@Override
	protected void myOnCreate(final Bundle savedInstanceState) {

		final ArrayList<SingleSpesa> uscite = CacheUscite.getSingleton().getAllUsciteForTest();
		final ArrayAdapter<SingleSpesa> adapter = new ArrayAdapter<SingleSpesa>(this, R.layout.row,
				uscite.toArray(new SingleSpesa[uscite.size()])) {
			@Override
			public View getView(final int position, View convertView, final ViewGroup parent) {
				ViewHolder viewHolder = null;
				if (convertView == null) {
					final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
					convertView = inflater.inflate(R.layout.row, null);
					viewHolder = new ViewHolder();
					viewHolder.listNomeTrs = (TextView) convertView.findViewById(R.id.listNomeTrs);
					viewHolder.listTipoTrs = (TextView) convertView.findViewById(R.id.listTipoTrs);
					viewHolder.listEuroTrs = (TextView) convertView.findViewById(R.id.listEuroTrs);
					viewHolder.listDataTrs = (TextView) convertView.findViewById(R.id.listDataTrs);
					convertView.setTag(viewHolder);
				} else {
					viewHolder = (ViewHolder) convertView.getTag();
				}
				final SingleSpesa uscita = getItem(position);

				viewHolder.listNomeTrs.setText(uscita.getNomeEntita() != null ? uscita.getNomeEntita() : "NO");
				viewHolder.listTipoTrs.setText(uscita.getCatSpese() != null ? uscita.getCatSpese().getNomeEntita() : "NO");
				viewHolder.listEuroTrs.setText(uscita.getinEuro() != 0 ? Double.toString(uscita.getinEuro()) : "NO");
				viewHolder.listDataTrs.setText(uscita.getData() != null ? uscita.getData() : "NO");
				convertView.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(final View v) {

						final Intent i = new Intent(ListaUscite.this, UsciteView.class);
						i.putExtra("idUscita", uscita.getidSpesa());
						try{
							startActivity(i);
						}catch (final Exception e) {
							final String msg = e.getMessage();
						}

					}
				});

				return convertView;
			}
		};

		final ListView list = (ListView) findViewById(R.id.listView1);
		list.setAdapter(adapter);

	}

	private static class ViewHolder {
		public TextView listNomeTrs;
		public TextView listTipoTrs;
		public TextView listEuroTrs;
		public TextView listDataTrs;
	}

}
